package com.qqt.csr.route.entity;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrPool;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.qqt.csr.route.vo.resp.WorkTimeRespO;
import lombok.*;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.stream.Collectors;

/**
 * 客服服务时间
 */
@Getter
@Setter
@Accessors(chain = true)
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "csr_service_time")
public class ServiceTime implements Serializable {
    /**
     * 主键
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 租户id
     */
    @TableField(value = "tenant_id")
    private String tenantId;

    /**
     * 创建时间
     */
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private Date createTime;

    /**
     * 创建人id
     */
    @TableField(value = "creator_id")
    private Long creatorId;

    /**
     * 是否移除：0 否；1 是
     */
    @TableField(value = "delete_flag")
    @TableLogic(value = "0", delval = "1")
    private Integer deleteFlag;

    /**
     * 节假日时间，以 | 分组，示例：2025-01-20 09:12:21_2025-01-20 09:12:21
     */
    @TableField(value = "holiday")
    private String holiday;

    /**
     * 服务名称
     */
    @TableField(value = "`name`")
    private String name;

    /**
     * 更新时间
     */
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    /**
     * 加班时间，以 | 分组，示例：2025-01-20 09:12:21_2025-01-20 09:12:21
     */
    @TableField(value = "work_overtime")
    private String workOvertime;

    /**
     * 工作时间，以 | 分组，示例：1-2-3-4-5-6-7 09:12:21_09:13:21
     * 1-2-3-4-5-6-7 表示周日到周一，1是周日，2是周一，以此类推
     */
    @TableField(value = "work_time")
    private String workTime;

    private static final long serialVersionUID = 1L;

    /**
     * 给定的时间是否在工作时间内
     *
     * @param date
     * @return
     */
    @JsonIgnore
    public boolean isWorkTime(Date date) {
        if (StringUtils.isNotBlank(holiday)) {
            //以 | 分组，示例：2025-01-20 09:12:21_2025-01-20 09:12:21
            String[] timeArray = StringUtils.split(holiday, StringPool.PIPE);
            for (String time : timeArray) {
                String[] dateArray = StringUtils.split(time, StringPool.UNDERSCORE);
                Date startTime = DateUtil.parseDateTime(dateArray[0]);
                Date endTime = DateUtil.parseDateTime(dateArray[1]);
                if (DateUtil.isIn(date, startTime, endTime)) {
                    return false;
                }
            }
        }

        if (StringUtils.isNotBlank(workTime)) {
            //以 | 分组，示例：1-2-3-4-5-6-7 09:12:21_09:13:21
            String[] weekTimeArray = StringUtils.split(workTime, StringPool.PIPE);
            int week = DateUtil.dayOfWeek(date);
            int currentTimeSecond = DateUtil.hour(date, true) * 60 * 60 + DateUtil.minute(date) * 60 + DateUtil.second(date);
            for (String weekTimeStr : weekTimeArray) {
                String[] weekTime = weekTimeStr.split(StringPool.SPACE);
                if (!weekTime[0].contains(String.valueOf(week))) {
                    continue;
                }
                String[] times = StringUtils.split(weekTime[1], StrPool.UNDERLINE);
                int startTime = DateUtil.timeToSecond(times[0]);
                int endTime = DateUtil.timeToSecond(times[1]);
                if (startTime <= currentTimeSecond && currentTimeSecond <= endTime) {
                    return true;
                }
            }
        }

        if (StringUtils.isNotBlank(workOvertime)) {
            //以 | 分组，示例：2025-01-20 09:12:21_2025-01-20 09:12:21
            String[] timeArray = StringUtils.split(workOvertime, StringPool.PIPE);
            for (String time : timeArray) {
                String[] dateArray = StringUtils.split(time, StringPool.UNDERSCORE);
                Date startTime = DateUtil.parseDateTime(dateArray[0]);
                Date endTime = DateUtil.parseDateTime(dateArray[1]);
                if (DateUtil.isIn(date, startTime, endTime)) {
                    return true;
                }
            }
        }

        return false;
    }
}